<?php
class ControllerInviterValidate extends Controller {
    public function index() {
        $this->document->setTitle($this->config->get('config_title'));
        $this->document->setDescription($this->config->get('config_meta_description'));

        $this->data['heading_title'] = $this->config->get('config_title');

        //setting parameters
        $authcode= $_GET["code"];
        
//		$this->data['gmail_clientid'] = $this->config->get('gmail_clientid');
		$clientid= $this->config->get('gmail_clientid');
		$clientsecret=$this->config->get('gmail_clientsecret');
		$this->data['hedef'] = $this->request->server['REMOTE_ADDR'];
        //$clientid='742523455277.apps.googleusercontent.com';
        //$clientsecret='AV32WZTkN4tUq8AXwNIyxfA4';
        $redirecturi=$this->url->link('inviter/validate');// 'http://localhost:8080/index.php?route=inviter/validate';
        $this->load->model('inviter/validate');
		$count=0;
		
		
		$fields=array(
            'code'=>  urlencode($authcode),
            'client_id'=>  urlencode($clientid),
            'client_secret'=>  urlencode($clientsecret),
            'redirect_uri'=>  urlencode($redirecturi),
            'grant_type'=>  urlencode('authorization_code')
        );
        //url-ify the data for the POST
        $fields_string='';
        foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
        $fields_string=rtrim($fields_string,'&');
        //open connection
        $ch = curl_init();
        //set the url, number of POST vars, POST data
        curl_setopt($ch,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
        curl_setopt($ch,CURLOPT_POST,5);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
        // Set so curl_exec returns the result instead of outputting it.
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //to trust any ssl certificates
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        //execute post
        $result = curl_exec($ch);
        //close connection
        curl_close($ch);
        //extracting access_token from response string
        $response=  json_decode($result);
        $accesstoken= $response->access_token;
        //passing accesstoken to obtain contact details
        $xmlresponse=  file_get_contents('https://www.google.com/m8/feeds/contacts/default/full?oauth_token='.$accesstoken);
        //reading xml using SimpleXML
        $xml=  new SimpleXMLElement($xmlresponse);
        $xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
        $result = $xml->xpath('//gd:email');
		
        foreach ($result as $title) {
		//$echo = $this->model_inviter_validate->addemail($title->attributes()->address);
			$count++;
        //    echo $title->attributes()->address . "<br><br>";
        }


$this->data['result'] = array();

$this->data['result']= $result;
$this->data['count']=$count;

        $this->load->model('account/customer');
$this->data['userid'] = $this->customer->getId(); //$this->customer->getFirstName();






        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/inviter/validate.php')) {
            $this->template = $this->config->get('config_template') . '/template/inviter/validate.php';
        } else {
            $this->template = 'default/template/inviter/validate.php';
        }

        $this->children = array(
            'common/column_left',
            'common/column_right',
            'common/content_top',
            'common/content_bottom',
            'common/footer',
            'common/header'
        );

        $this->response->setOutput($this->render());
    }
}
?>